#include <stdio.h>
#include <string.h>

unsigned char hex_bcd(unsigned char);
unsigned char bcd_hex(unsigned char);

int hex_ascii(const char * hex, char * ascii);
int ascii_hex(char * ascii, char * hex);
char hex_char(char ch);

#define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
#define BIN_TO_BCD(val)	((((val)/10)<<4) + (val)%10)	// 取十位,移到高位, 取个位


#define DEC_BCD(value)	((((value)/10)*16) + ((value)%10))
#define	HEX_BCD(value)	((((value)/16)*10) + ((value)%16))

#define BCD_DEC(value)	((((value)/16)*10) + ((value)%16))
#define BCD_HEX(value)	((((value)/10)*16) + ((value)%10))

#define HEX_DEC(value)	((unsigned int)(value))
#define DEC_HEX(result,value)	sprintf(result, "%x", value)
#define DEC_OCT(result,value)	sprintf(result, "%o", value)

int _main(void)
{
	unsigned char hex;
	unsigned char bcd;
	unsigned char ret1;
	unsigned char ret2;
	int dec = 12;

	hex = 0x0c;	// 相当于BCD码的12
	bcd = 0x12;	// 相当于hex码的0x0c

//	printf("bcd to dec = %d\n", BCD_DEC(15));
//	printf("dec to bcd = %d\n", DEC_BCD(15));

	printf("bcd to dec = %d\n", BCD_DEC(0x20));
	printf("bcd to dec = %d\n", BCD_TO_BIN(0x20));

	printf("dec to bcd = %d\n", DEC_BCD(15));
	printf("dec to bcd = %x\n", DEC_BCD(12));

	printf("dec to bcd = %d\n", HEX_BCD(0x15));
	printf("dec to bcd = %x\n", BCD_HEX(12));
}

unsigned char hex_bcd(unsigned char a)
{
	unsigned char th, tl;

	th = a / 10;
	tl = a - th * 10;

	a = (th << 4) + tl;
	
	return a;
}

unsigned char bcd_hex(unsigned char a)
{
	unsigned char th, tl, temp;

		th = (a & 0xf0) >> 4;
		tl = a & 0x0f;

		a = th * 10 + tl;

		return a;
}

int hex_ascii(const char * hex, char * ascii)
{
	int len = strlen(hex), tlen, i, cnt;

	for (i = 0, cnt = 0, tlen = 0; i < len; i++) {
		char c = toupper(hex[i]);

		if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
			char t = (c >= 'A') ? c - 'A' + 10 : c - '0';
			if (cnt)
				ascii[tlen++] += t, cnt = 0;
			else
				ascii[tlen] = t << 4, cnt = 1;
		}
	}

	return tlen;
}

int ascii_hex(char * ascii, char * hex)
{
	int i, len = strlen(ascii);
	char ch_hex[] = "0123456789ABCDEF";

	for (i = 0; i < len; i++) {
		hex[i * 3] = ch_hex[((char)ascii[i]) >> 4];
		hex[i * 3 + 1] = ch_hex[((char)ascii[i]) & 0xf];
		hex[i * 3 + 2] = ' ';
	}
	hex[len * 3] = '\0';

	return len * 3;
}

char hex_char(char ch)
{
	if ((ch >= '0') && (ch <= '9'))
		return ch - 0x30;
	else if ((ch >= 'A') && (ch <= 'F'))
		return ch - 'A' + 10;
	else if ((ch >= 'a') && (ch <= 'f'))
		return ch - 'a' + 10;
	else
		return -1;
}

int main(void)
{
	char hex[3] = {0x11, 0x22, 0x33};
	char ascii[4];

	printf("tlen = %d \n", hex_ascii(hex, ascii));

	printf("char = %c \n", hex_char('i'));
	return 0;
}

void char_to_hex(char * dest, unsigned char * src)
{
	int i;
	unsigned char hex = 0;
	int index = 0;

	for(i=0;i<strlen((char *)src);i=i+2)
	{
		if((src[i]>='0')&&(src[i]<='9'))
		{
			hex = (src[i]-'0')<<4;
		}
		else if((src[i]>='A')&&(src[i]<='F'))
		{
			hex = (src[i]-'A'+10)<<4;
		}

		if((src[i+1]>='0')&&(src[i+1]<='9'))
		{
			hex |= (src[i+1]-'0');
		}
		else if((src[i+1]>='A')&&(src[i+1]<='F'))
		{
			hex |= (src[i+1]-'A'+10);
		}

		dest[index++] = hex;
	}
}
